package org.extdev.device.ltr;

import org.extdev.libraries.ltr.LTR11APIConstants;
import org.extdev.protocol.unsigned.UnsignedByte;

public class LTR11Channel {
	private int physicalChannelNo = 1;
	private LTR11VirtualChannelsMode channelMode;
	private LTR11Scale channelScale;
	
	public int getPhysicalChannelNo() {
		return physicalChannelNo;
	}
	
	public void setPhysicalChannelNo(int value) {
		if (value > LTR11APIConstants.LTR11_MAX_CHANNEL || value < 1) {
			throw new IllegalArgumentException("Physical channel must be in range of [1..32]");
		}
		this.physicalChannelNo = value;
	}
	
	public LTR11VirtualChannelsMode getChannelMode() {
		return channelMode;
	}
	
	public void setChannelMode(LTR11VirtualChannelsMode value) {
		this.channelMode = value;
	}
	
	public LTR11Scale getChannelScale() {
		return channelScale;
	}
	
	public void setChannelScale(LTR11Scale value) {
		this.channelScale = value;
	}
	
	public byte toByte() {
		UnsignedByte unsignedByte = new UnsignedByte();
		unsignedByte
			.or((byte)channelScale.ordinal())
			.leftShift(2)
			.or((byte)channelMode.ordinal())
			.leftShift(4)
			.or((byte)physicalChannelNo);
		return unsignedByte.getByte();
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Ch|p:").append(physicalChannelNo).append("|").append(channelMode).append("|").append(channelScale);
		return builder.toString();
	}
}
